<?php
namespace app\admin\controller;
use think\Input;
class Order extends Common{
    public function initialize(){
        parent::initialize();   
        $this->assign('shoplist',cache('shoplist'));   
        $this->assign('agentlist',cache('agentlist'));  
        $this->assign('userlist',cache('userlist'));  
        $this->assign('cionlist',cache('cionlist')); 
    }
    public function index(){
    	if(request()->isPost()){ 
            $where=[];
            $map='';
            $shopid = input('post.shopid');
            $shopinfo = db('shop')->where('id',$shopid)->find();
            if($shopinfo['level'] == 2){
                if($shopid) $where['sagentid']=$shopid; 
            }else{
                if($shopid) $where['shopid']=$shopid; 
            }
            // var_dump($this->seller['level']);die;
            // if($shopid) $where['shopid']=$shopid; 
            $timess = input('post.timess');            
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1]); 
                if($starttime >= $endtime){
                    return ['code'=>-1,'msg'=>'开始日期需要晚于结束日期！'];
                }   
                $map= [$starttime,$endtime];  
            }
            $timequan = input('post.timequan');
            if($timequan) $map= ''.$timequan.''; 
            $orderid = input('post.orderid');
            if($orderid) $where['orderid']=$orderid;  
            $orderid_out = input('post.orderid_out');
            if($orderid_out) $where['orderid_out']=$orderid_out;   
            $userid = input('post.userid');
            if($userid) $where['o.userid']=$userid;  
            $channelid = input('post.channelid');
            if($channelid) $where['o.channelid']=$channelid;  
            $agentid = input('post.agentid');
            if($agentid) $where['o.agentid']=$agentid;  

            $state = input('post.state');
            if($state == '-1'){
                if($state) $where['o.state']=0;
            }else{
                if($state) $where['o.state']=$state;
            }
            $points = input('post.points');
            if($points)$where['points']=$points;

            $paytype = input('post.paytype');
            if($paytype)$where['ub.name_en']=$paytype;  
               
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('channel_order')->alias('o')
                ->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')
                ->join(config('database.prefix') . 'users u', 'o.userid = u.id', 'left')
                ->field('o.*,ub.name as payname,ub.bank_no,ub.bank_name,u.username')
                ->where($where)->whereTime('o.addtime',$map)->order('o.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();   
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                if($list['data'][$k]['paytype'] == 8){
                    $list['data'][$k]['payname'] ='<span class="blue">APP支付宝</span>';
                }elseif ($list['data'][$k]['paytype'] == 9) {
                    $list['data'][$k]['payname'] ='<span class="green">APP微信</span>';
                }
                if($v['paytime']) $list['data'][$k]['paytime'] = date('Y-m-d H:i',$v['paytime']);
                if($v['state']===0){ 
                    $list['data'][$k]['state_name'] ='<span class="red">待支付</span>';
                }else if($v['state']===1){
                    $list['data'][$k]['state_name'] ='<span class="blue">待确认</span>';
                }else if($v['state']===2){
                    $list['data'][$k]['state_name'] ='<span class="green">已确认</span>';
                }else if($v['state']===3){
                    $list['data'][$k]['state_name'] ='<span class="hui">已取消</span>';
                }else if($v['state']===-1){
                    $list['data'][$k]['state_name'] ='<span class="hui">已删除</span>';
                } 
                if($v['points']===0){
                    $list['data'][$k]['points_name'] ='<span class="orange">待上分</span>';
                }else if($v['points']===1){
                    $list['data'][$k]['points_name'] ='<span class="green">自动上分</span>';
                }else if($v['points']===2){
                    $list['data'][$k]['points_name'] ='<span class="green">手动上分</span>';
                }
                if($v['fei_user_jie']===0) $list['data'][$k]['fei_user']='<span class="orange" title="待结算">'.$v['fei_user'].'</span>';
                if($v['fei_shop_jie']===0) $list['data'][$k]['fei_shop']='<span class="orange" title="待结算">'.$v['fei_shop'].'</span>';
                if($v['fei_agent_jie']===0) $list['data'][$k]['fei_agent']='<span class="orange" title="待结算">'.$v['fei_agent'].'</span>';
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        } 
        // 订单汇总
        // $wheres=[];
        // $maps='';
        // $_shopid = input('get.shopid');
        // if($_shopid){
        //     $wheres['shopid']=$_shopid; 
        //     $this->assign('_shopid',$_shopid);
        // }
        // $_timess = input('get.timess');            
        // if($_timess){ 
        //     $_timess=explode(" - ",$_timess);
        //     $starttime=strtotime($_timess[0]);
        //     $endtime=strtotime($_timess[1])+86399;    
        //     $maps= [$starttime,$endtime];  
        // }
        // $_timequan = input('get.timequan');
        // if($_timequan) $maps= ''.$_timequan.''; 
        // $_orderid = input('get.orderid');
        // if($_orderid) $wheres['orderid']=$_orderid;  
        // $_orderid_out = input('get.orderid_out');
        // if($_orderid_out) $wheres['orderid_out']=$_orderid_out;   
        // $_userid = input('get.userid');
        // if($_userid) $wheres['userid']=$_userid;  
        // $_channelid = input('get.channelid');
        // if($_channelid) $wheres['channelid']=$_channelid;  
        // $_agentid = input('get.agentid');
        // if($_agentid) $wheres['agentid']=$_agentid;  

        // $_state = input('get.state');
        // if($_state)$wheres['state']=$_state;
        // $_points = input('get.points');
        // if($_points)$wheres['points']=$_points;

        // $_paytype = input('get.paytype');
        // if($_paytype)$wheres['name_en']=$_paytype; 
        $payorder=[];
        $payorder['no']=db('channel_order')->where('state',0)->where($wheres)->sum('money');
        $payorder['que']=db('channel_order')->where('state',1)->where($wheres)->sum('money');
        $payorder['yes']=db('channel_order')->where('state',2)->where($wheres)->sum('money'); 
        $payorder['feishop']=db('channel_order')->where('state',2)->where($wheres)->sum('fei_shop'); 
        $payorder['feiagent']=db('channel_order')->where('state',2)->where($wheres)->sum('fei_agent'); 
        $payorder['feiuser']=db('channel_order')->where('state',2)->where($wheres)->sum('fei_user'); 
        $this->assign('payorder',$payorder);
        $this->assign('title','订单管理');
        return $this->fetch();
    } 
    // 小列表
    public function ordermin(){
        $this->assign('userid',input('userid'));
        $this->assign('channelid',input('channelid'));
        $this->assign('shopid',input('shopid')); 
        $this->assign('agentid',input('agentid')); 
        return $this->fetch();
    }
    // 成功订单
    public function oklist(){ 
         // 订单汇总
        $payorder=[]; 
        $payorder['yes']=db('channel_order')->where('state',2)->sum('money'); 
        $payorder['feishop']=db('channel_order')->where('state',2)->sum('fei_shop'); 
        $payorder['feiagent']=db('channel_order')->where('state',2)->sum('fei_agent'); 
        $payorder['feiuser']=db('channel_order')->where('state',2)->sum('fei_user'); 
        $this->assign('payorder',$payorder);
        return $this->fetch();
    }
    public function reback(){ 
        if(request()->isPost()){  
            $order=db('channel_order')->where(array('id'=>input('id'),'state'=>2,'points'=>0))->find();
            if($order){
                if(db('channel_order')->where('orderid',$order['orderid'])->update(['state'=>0,'paytime'=>''])!==false){
                    db('channel')->where('id',$order['channelid'])->where('shopappid',$order['shopappid'])->setDec('money_total',$order['money']);
                    return ['code'=>1,'msg'=>'订单变为未支付！'];
                }else{
                    return ['code'=>-1,'msg'=>'操作失败！']; 
                } 
            }else{
                return ['code'=>-1,'msg'=>'订单异常或不允许变更！'];
            }  
        }
    }
    // 删除订单
    public function dellist(){  
        return $this->fetch();
    }
    // 结算单 
    public function settlement(){  
        if(request()->isPost()){ 
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('jiesuan')->alias('j')
                ->join(config('database.prefix') . 'users u', 'j.userid = u.id', 'left')
                ->join(config('database.prefix') . 'shop s', 'j.shopid = s.id', 'left')
                ->field('j.*,u.mobile,s.mobile as smobile,s.username as susername')
                ->order('j.addtime desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))->toArray(); 
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']);
                if($v['type']=='user'){
                    $list['data'][$k]['typename']='码商'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='agent'){
                    $list['data'][$k]['typename']='代理'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='shop'){
                    $list['data'][$k]['typename']='商户'; 
                    $list['data'][$k]['username']=$v['susername']; 
                    $list['data'][$k]['mobile']=$v['smobile']; 
                }
                if($v['state']===0){
                    if($v['type']=='user'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='<span class="green">+'.$list['data'][$k]['money_sf'].'</span>'; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='<span class="red">-'.$list['data'][$k]['money_sf'].'</span>'; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['money_sf']='<span class="red">-'.$v['money_s'].'</span>'; 
                    }
                    $list['data'][$k]['state_name']='<span class="red">待处理</span>'; 
                }else{
                    if($v['type']=='user'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='+'.$list['data'][$k]['money_sf']; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='-'.$list['data'][$k]['money_sf']; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['money_sf']='-'.$v['money_s']; 
                    }
                    $list['data'][$k]['state_name']='<span class="green">已处理</span>'; 
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        // 今日未结算
        $money_today=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','today')->sum('money');
        $order_today=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','today')->count();
        $this->assign('money_today',$money_today);
        $this->assign('order_today',$order_today);
        // 昨日未结算
        $money_yesterday=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','yesterday')->sum('money');
        $order_yesterday=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','yesterday')->count();
        $this->assign('money_yesterday',$money_yesterday);
        $this->assign('order_yesterday',$order_yesterday);
        // 本周未结算
        $money_week=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','week')->sum('money');
        $order_week=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','week')->count();
        $this->assign('money_week',$money_week);
        $this->assign('order_week',$order_week);
        // 上周未结算
        $money_lastweek=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','last week')->sum('money');
        $order_lastweek=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','last week')->count();
        $this->assign('money_lastweek',$money_lastweek);
        $this->assign('order_lastweek',$order_lastweek);
        // 所有未结算        
        $money_all_no=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->sum('money');
        $order_all_no=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->count();
        $this->assign('money_all_no',$money_all_no);
        $this->assign('order_all_no',$order_all_no);
        // 所有已结算
        $money_all_yes=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>1,'fei_shop_jie' =>1))->sum('money');
        $order_all_yes=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>1,'fei_shop_jie' =>1))->count();
        $this->assign('money_all_yes',$money_all_yes);
        $this->assign('order_all_yes',$order_all_yes);  
        // 结算汇总
        $money_get_no=db('jiesuan')->where(array('type' =>'user','state' =>0))->field('SUM(money_a) as moneya,SUM(money_s) as moneys')->select(); 
        $this->assign('money_get_no',$money_get_no[0]['moneya']-$money_get_no[0]['moneys']);

        $money_get_yes=db('jiesuan')->where(array('type' =>'user','state' =>1))->field('SUM(money_a) as moneya,SUM(money_s) as moneys')->select(); 
        $this->assign('money_get_yes',$money_get_yes[0]['moneya']-$money_get_yes[0]['moneys']);

        $money_pay_no_shop=db('jiesuan')->where(array('type' =>'shop','state' =>0))->field('SUM(money_a) as moneya,SUM(money_s) as moneys')->select();         
        $money_pay_no_agent=db('jiesuan')->where(array('type' =>'agent','state' =>0))->sum('money_s');
        $this->assign('money_pay_no',$money_pay_no_shop[0]['moneya']-$money_pay_no_shop[0]['moneys']+$money_pay_no_agent);


        $money_pay_yes_shop=db('jiesuan')->where(array('type' =>'shop','state' =>1))->field('SUM(money_a) as moneya,SUM(money_s) as moneys')->select();         
        $money_pay_yes_agent=db('jiesuan')->where(array('type' =>'agent','state' =>1))->sum('money_s');
        $this->assign('money_pay_yes',$money_pay_yes_shop[0]['moneya']-$money_pay_yes_shop[0]['moneys']+$money_pay_yes_agent);


        return $this->fetch();
    } 
    // 小结算单 
    public function settlementmin(){  
        if(request()->isPost()){  
            $shopid = input('post.shopid')?input('post.shopid'):0;
             if($this->seller['level'] == 2){
                if($shopid) $where['sagentid']=$shopid; 
            }else{
                if($shopid) $where['shopid']=$shopid; 
            }
            // if($shopid) $where['shopid']=$shopid; 
            $userid = input('post.userid')?input('post.userid'):0;
            if($userid) $where['userid']=$userid;  
            $page =input('page')?input('page'):1;

            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('jiesuan')->alias('j')
                ->join(config('database.prefix') . 'users u', 'j.userid = u.id', 'left')
                ->join(config('database.prefix') . 'shop s', 'j.shopid = s.id', 'left')->where($where)
                ->field('j.*,u.mobile,s.mobile as smobile,s.username as susername')
                ->order('j.addtime desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))->toArray(); 
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']);
                if($v['type']=='user'){
                    $list['data'][$k]['typename']='码商'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='agent'){
                    $list['data'][$k]['typename']='代理'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='shop'){
                    $list['data'][$k]['typename']='商户'; 
                    $list['data'][$k]['username']=$v['susername']; 
                    $list['data'][$k]['mobile']=$v['smobile']; 
                }
                if($v['state']===0){
                    if($v['type']=='user'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='<span class="green">+'.$list['data'][$k]['money_sf'].'</span>'; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='<span class="red">-'.$list['data'][$k]['money_sf'].'</span>'; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['money_sf']='<span class="red">-'.$v['money_s'].'</span>'; 
                    }
                    $list['data'][$k]['state_name']='<span class="red">待处理</span>'; 
                }else{ 
                    if($v['type']=='user'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='+'.$list['data'][$k]['money_sf']; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='-'.$list['data'][$k]['money_sf']; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['money_sf']='-'.$v['money_s']; 
                    }
                    $list['data'][$k]['state_name']='<span class="green">已处理</span>'; 
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        } 
        $this->assign('shopid',input('shopid'));
        $this->assign('userid',input('userid')); 
        return $this->fetch();
    } 
    
    public function pass_jiesuan(){
        if(request()->isPost()){  
            if(db('jiesuan')->where(array('id'=>input('id'),'state'=>0))->update(['state'=>1])!==false){
                return ['code'=>1,'msg'=>'操作成功！'];
            }else{
                return ['code'=>-1,'msg'=>'操作失败！'];
            } 
        }
    }
    // 结算操作——码商
    public function jieshu(){  
        if(request()->isPost()){ 
            $map='';
            $timequ = input('post.timequ');            
            if($timequ) $map= ''.$timequ.''; 
            $list_user=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0))->whereTime('addtime',$map)
                ->group('userid')->order('id desc')->select();
            if($list_user){
                foreach ($list_user as $k=>$v){ 
                    $order_user=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'userid'=>$v['userid']))->whereTime('addtime',$map)->select(); 
                    $money_s[$k]=0;
                    $money_a[$k]=0;
                    $number[$k]=0;
                    foreach ($order_user as $kk=>$vv){
                        $money_s[$k] +=$vv['fei_user'];
                        $money_a[$k] +=$vv['money'];
                        $number[$k] +=1;
                        db('channel_order')->where('id',$vv['id'])->update(['fei_user_jie'=>1,'fei_user_time'=>time()]);
                    } 
                    $username[$k]=db('users')->where('id',$v['userid'])->value('username');
                    $datas[$k]= array(
                        'orderid' =>'J'.rand_string(8,1).'S'.$v['userid'], 
                        'money_s' =>$money_s[$k], 
                        'money_a' =>$money_a[$k], 
                        'number' =>$number[$k], 
                        'state' =>0, 
                        'type' =>'user', 
                        'userid' =>$v['userid'], 
                        'username' =>$username[$k], 
                        'addtime' =>time(), 
                    ); 
                    db('jiesuan')->insertGetId($datas[$k]);
                }
                return $result = ['code'=>1,'msg'=>'码商结算完成，即将结算商户！'];
            }else{
                return $result = ['code'=>0,'msg'=>'无需结算!'];
            }  
        } 
    }
    // 结算商户
    public function jieshu_shop(){  
        if(request()->isPost()){ 
            $map='';
            $timequ = input('post.timequ');            
            if($timequ) $map= ''.$timequ.''; 
            $list_shop=db('channel_order')->where(array('state' =>2,'fei_shop_jie' =>0))->whereTime('addtime',$map)
                ->group('shopid')->order('id desc')->select();
            if($list_shop){
                foreach ($list_shop as $k=>$v){ 
                    $order_shop=db('channel_order')->where(array('state' =>2,'fei_shop_jie' =>0,'shopid'=>$v['shopid']))->whereTime('addtime',$map)->select();
                    $money_s[$k]=0;
                    $money_a[$k]=0;
                    $number[$k]=0;
                    foreach ($order_shop as $kk=>$vv){
                        $money_s[$k] +=$vv['fei_shop'];
                        $money_a[$k] +=$vv['money'];
                        $number[$k] +=1;
                        db('channel_order')->where('id',$vv['id'])->update(['fei_shop_jie'=>1,'fei_shop_time'=>time()]); 
                    } 
                    $datas[$k]= array(
                        'orderid' =>'J'.rand_string(8,1).'S'.$v['shopid'], 
                        'money_s' =>$money_s[$k], 
                        'money_a' =>$money_a[$k], 
                        'number' =>$number[$k], 
                        'state' =>0, 
                        'type' =>'shop', 
                        'shopid' =>$v['shopid'], 
                        'shopname' =>$v['shopname'], 
                        'addtime' =>time(), 
                    ); 
                    db('jiesuan')->insertGetId($datas[$k]);
                }
                return $result = ['code'=>1,'msg'=>'商户结算完成，即将结算代理!'];
            }else{
                return $result = ['code'=>0,'msg'=>'无需结算!'];
            }  
        } 
    }
    //结算代理
    public function jieshu_agent(){  
        if(request()->isPost()){ 
            $map='';
            $timequ = input('post.timequ');            
            if($timequ) $map= ''.$timequ.''; 
            $list_user=db('channel_order')->where(array('state' =>2,'fei_agent_jie' =>0))->whereTime('addtime',$map)
                ->group('agentid')->order('id desc')->select();
            if($list_user){
                foreach ($list_user as $k=>$v){ 
                    $order_user=db('channel_order')->where(array('state' =>2,'fei_agent_jie' =>0,'agentid'=>$v['agentid']))->whereTime('addtime',$map)->select(); 
                    $money_s[$k]=0;
                    $money_a[$k]=0;
                    $number[$k]=0;
                    foreach ($order_user as $kk=>$vv){
                        $money_s[$k] +=$vv['fei_agent'];
                        $money_a[$k] +=$vv['money'];
                        $number[$k] +=1;
                        db('channel_order')->where('id',$vv['id'])->update(['fei_agent_jie'=>1,'fei_agent_time'=>time()]);
                    } 
                    if($v['agentid']){
                        $username[$k]=db('users')->where('id',$v['agentid'])->value('username');
                        $datas[$k]= array(
                            'orderid' =>'J'.rand_string(8,1).'S'.$v['agentid'], 
                            'money_s' =>$money_s[$k], 
                            'money_a' =>$money_a[$k], 
                            'number' =>$number[$k], 
                            'state' =>0, 
                            'type' =>'agent', 
                            'userid' =>$v['agentid'], 
                            'username' =>$username[$k], 
                            'addtime' =>time(), 
                        ); 
                        db('jiesuan')->insertGetId($datas[$k]);
                    } 
                }
                return $result = ['code'=>1,'msg'=>'结算结束'];
            }else{
                return $result = ['code'=>0,'msg'=>'无需结算!'];
            }  
        } 
    }

     //批量清除订单
    public function delorderlist()
    {
         if(request()->isPost()){ 
            $where=[];
            $map='';
            $shopid = input('post.shopid');
            if($shopid) $where['shopid']=$shopid; 
            $timess = input('post.timess');            
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1]);  
                if($starttime >= $endtime){
                    return $result = ['code'=>-1,'msg'=>'开始日期要早于结束日期!','rel'=>1];
                }  
                $map= [$starttime,$endtime];  
            }
            $timequan = input('post.timequan');
            if($timequan) $map= ''.$timequan.''; 
            $orderid = input('post.orderid');
            if($orderid) $where['orderid']=$orderid;  
            $orderid_out = input('post.orderid_out');
            if($orderid_out) $where['orderid_out']=$orderid_out;   
            $userid = input('post.userid');
            if($userid) $where['o.userid']=$userid;  
            $channelid = input('post.channelid');
            if($channelid) $where['o.channelid']=$channelid;  
            $agentid = input('post.agentid');
            if($agentid) $where['o.agentid']=$agentid;  

            $state = input('post.state');
            if($state == '-1'){
                if($state)$where['o.state']=0;
            }else{
                if($state)$where['o.state']=$state;
            }
            

            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('channel_order')->alias('o')
                ->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')
                ->join(config('database.prefix') . 'users u', 'o.userid = u.id', 'left')
                ->field('o.*,ub.name as payname,ub.bank_no,ub.bank_name,u.username')
                ->where($where)->whereTime('o.addtime',$map)->order('o.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();   
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                if($v['paytime']) $list['data'][$k]['paytime'] = date('Y-m-d H:i',$v['paytime']);
                if($v['state']===0){ 
                    $list['data'][$k]['state_name'] ='<span class="red">待支付</span>';
                }else if($v['state']===1){
                    $list['data'][$k]['state_name'] ='<span class="blue">待确认</span>';
                }else if($v['state']===2){
                    $list['data'][$k]['state_name'] ='<span class="green">已确认</span>';
                }else if($v['state']===3){
                    $list['data'][$k]['state_name'] ='<span class="hui">已取消</span>';
                }else if($v['state']===-1){
                    $list['data'][$k]['state_name'] ='<span class="hui">已删除</span>';
                } 
                if($v['points']===0){
                    $list['data'][$k]['points_name'] ='<span class="orange">待上分</span>';
                }else if($v['points']===1){
                    $list['data'][$k]['points_name'] ='<span class="green">自动上分</span>';
                }else if($v['points']===2){
                    $list['data'][$k]['points_name'] ='<span class="green">手动上分</span>';
                }
                if($v['fei_user_jie']===0) $list['data'][$k]['fei_user']='<span class="orange" title="待结算">'.$v['fei_user'].'</span>';
                if($v['fei_shop_jie']===0) $list['data'][$k]['fei_shop']='<span class="orange" title="待结算">'.$v['fei_shop'].'</span>';
                if($v['fei_agent_jie']===0) $list['data'][$k]['fei_agent']='<span class="orange" title="待结算">'.$v['fei_agent'].'</span>';
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
    

        return $this->fetch();
    }

     public function del()
    {
        if(request()->isPost()){
            $where=[];
            $map='';
            $shopid = input('post.shopid');
            if($shopid) $where['shopid']=$shopid; 
            $timess = input('post.timess');            
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1]);  
                if($starttime >= $endtime){
                    return $result = ['code'=>-1,'msg'=>'开始日期要早于结束日期!','rel'=>1];
                }  
                $map= [$starttime,$endtime];  
            }
            $orderid = input('post.orderid');
            if($orderid) $where['orderid']=$orderid;  
            $orderid_out = input('post.orderid_out');
            if($orderid_out) $where['orderid_out']=$orderid_out;    
            $state = input('post.state');
            if($state == '-1'){
                if($state)$where['state']=0;
            }else{
                if($state)$where['state']=$state;
            }
            $del = db('channel_order')->where($where)->whereTime('addtime',$map)->delete();      
            if($del){
                return $result = ['code'=>1,'msg'=>'删除成功！!','rel'=>1];
                // $this->success('删除成功！','delorderlist');
            }else {
                return $result = ['code'=>0,'msg'=>'删除失败！!','rel'=>1];
                // $this->error('删除失败！','delorderlist');
            }
        }
    }

   public function totals(){
        if(request()->isPost()){ 
            $where=[];
            $map='';
            $shopid = input('post.shopid');
            if($shopid) $where['o.shopid']=$shopid;
            $channelid = input('post.channelid');
            if($channelid) $where['o.channelid']=$channelid;  
            $state = input('post.state');
            if($state) $where['o.state']=$state;  
            $timess = input('post.timess');            
            // if($timess){ 
            //     $timess=explode(" - ",$timess);
            //     $starttime=strtotime($timess[0]);
            //     $endtime=strtotime($timess[1])+86399;    
            //     $map= [$starttime,$endtime];  
            // }
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1]); 
                if($starttime >= $endtime){
                    return ['code'=>-1,'msg'=>'开始日期需要早于结束日期！'];
                }   
                $map= [$starttime,$endtime];  
            }
            $timequan = input('post.timequan');
            if($timequan) $map= ''.$timequan.''; 
            $orderid = input('post.orderid');
            if($orderid) $where['o.orderid']=$orderid;  
            $orderid_out = input('post.orderid_out');
            if($orderid_out) $where['o.orderid_out']=$orderid_out; 
            $paytype = input('post.paytype');
            if($paytype)$where['ub.name_en']=$paytype; 
            $state = input('post.state');
            if($state === "-1"){
                if($state)$where['o.state']="0";
            }else{
               if($state)$where['o.state']=$state; 
            } 
            // $list=db('channel_order')->alias('o')
            //     ->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')
            //     ->join(config('database.prefix') . 'users u', 'o.userid = u.id', 'left')
            //     ->field('o.*,ub.name as payname,ub.bank_no,ub.bank_name,u.username')
            //     ->where($where)->whereTime('o.addtime',$map)
            //     ->select();
            
            // $userid = input('post.userid');
            // if($userid) $where['o.userid']=$userid;  
            // $agentid = input('post.agentid');
            // if($agentid) $where['o.agentid']=$agentid;  
            // $points = input('post.points');
            // if($points)$where['points']=$points;            
            
            // $payorder['no']=
            //     db('channel_order')->alias('o')
            //     ->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')
            //     ->where('state',0)
            //     ->where($where)
            //     ->whereTime('addtime',$map)
            //     ->sum('money');

            $payorder=[];
            $payorder['no']=db('channel_order')->alias('o')->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')->where('state',0)->where($where)->whereTime('o.addtime',$map)->sum('money');
            $payorder['yes']=db('channel_order')->alias('o')->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')->where('state',2)->where($where)->whereTime('o.addtime',$map)->sum('money');
            $payorder['feishop']=db('channel_order')->alias('o')->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')->where('state',2)->where($where)->whereTime('o.addtime',$map)->sum('fei_shop'); 
            $payorder['shop'] = $payorder['yes']-$payorder['feishop'];
            $payorder['feiagent']=db('channel_order')->alias('o')->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')->where('state',2)->where($where)->whereTime('o.addtime',$map)->sum('fei_agent'); 
            $payorder['feiuser']=db('channel_order')->alias('o')->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')->where('state',2)->where($where)->whereTime('o.addtime',$map)->sum('fei_user');
            $payorder['order_totalnum']=count(db('channel_order')->alias('o')->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')->where($where)->whereTime('o.addtime',$map)->select());
            $payorder['order_nonum']=count(db('channel_order')->alias('o')->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')->where($where)->where('state',0)->whereTime('o.addtime',$map)->select());
            $payorder['order_yesnum']=count(db('channel_order')->alias('o')->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')->where($where)->where('state',2)->whereTime('o.addtime',$map)->select());
            $payorders[0]=$payorder;

            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$payorders,'rel'=>1];
        } 
    }

     //新订单结算
    public function _settle(){
        if(request()->isPost()){ 
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('jiesuan')->alias('j')
                ->join(config('database.prefix') . 'users u', 'j.userid = u.id', 'left')
                ->join(config('database.prefix') . 'shop s', 'j.shopid = s.id', 'left')
                ->field('j.*,u.mobile,s.mobile as smobile,s.username as susername')
                ->order('j.addtime desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))->toArray(); 
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']);
                if($v['type']=='user'){
                    $list['data'][$k]['typename']='码商'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='agent'){
                    $list['data'][$k]['typename']='代理'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='shop'){
                    $list['data'][$k]['typename']='商户'; 
                    $list['data'][$k]['username']=$v['susername']; 
                    $list['data'][$k]['mobile']=$v['smobile']; 
                }
                if($v['state']===0){
                    if($v['type']=='user'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='<span class="green">+'.$list['data'][$k]['money_sf'].'</span>'; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='<span class="red">-'.$list['data'][$k]['money_sf'].'</span>'; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['money_sf']='<span class="red">-'.$v['money_s'].'</span>'; 
                    }
                    $list['data'][$k]['state_name']='<span class="red">待处理</span>'; 
                }else{
                    if($v['type']=='user'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='+'.$list['data'][$k]['money_sf']; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='-'.$list['data'][$k]['money_sf']; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['money_sf']='-'.$v['money_s']; 
                    }
                    $list['data'][$k]['state_name']='<span class="green">已处理</span>'; 
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        if(!$shoplist){
            $shoplist=db('shop')->where('open',1)->order('id')->select();
            cache('shoplist',$shoplist);
        }
        return $this->fetch();
    }

    public function handle_settle(){
        if(request()->isPost()){
            $where=[];
            $map='';
            $shopid = input('post.shopid');
            if($shopid){
                $where['shopid']=$shopid; 
            }else{
                return $result = ['code'=>-1,'msg'=>'请选择商户!','rel'=>1];
            } 
            $timess = input('post.timess');
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1]); 
                if($starttime >= $endtime){
                    return $result = ['code'=>-1,'msg'=>'开始日期要早于结束日期!','rel'=>1]; 
                } 
                //2019-05-17 13:36:53 - 2019-05-17 13:37:48  
                $map= [$starttime,$endtime];  
            }
            $orderid = input('post.orderid');
            if($orderid) $wheres['orderid']=$orderid;  
            $orderid_out = input('post.orderid_out');
            if($orderid_out) $wheres['orderid_out']=$orderid_out;  

            $list_shop=db('channel_order')
                            ->where(array('state' =>2,'fei_shop_jie' =>0))
                            ->whereTime('addtime',$map)
                            ->where($where)
                            ->where($wheres)
                            ->order('id desc')
                            ->select();
            $list_agent=db('channel_order')
                            ->where(array('state' =>2,'fei_agent_jie' =>0))
                            ->whereTime('addtime',$map)
                            ->where($where)
                            ->where($wheres)
                            ->group('agentid')
                            ->select();
            $list_user=db('channel_order')
                            ->where(array('state' =>2,'fei_user_jie' =>0))
                            ->whereTime('addtime',$map)
                            ->where($where)
                            ->where($wheres)
                            ->group('userid')
                            ->select();

            if(!empty($list_shop) && !empty($list_agent) && !empty($list_user)){
                if($list_shop){
                        $money_s[$k]=0;
                        $money_a[$k]=0;
                        $number[$k]=0;
                        foreach ($list_shop as $kk=>$vv){
                            $money_s[$k] +=$vv['fei_shop'];
                            $money_a[$k] +=$vv['money'];
                            $number[$k] +=1;
                            db('channel_order')->where('id',$vv['id'])->update(['fei_shop_jie'=>1,'fei_shop_time'=>time()]); 
                        } 
                        $datas[$k]= array(
                            'orderid' =>'J'.rand_string(8,1).'S'.$v['shopid'], 
                            'money_s' =>$money_s[$k], 
                            'money_a' =>$money_a[$k], 
                            'number' =>$number[$k], 
                            'state' =>0, 
                            'type' =>'shop', 
                            'shopid' =>$vv['shopid'], 
                            'shopname' =>$vv['shopname'], 
                            'addtime' =>time(), 
                        ); 
                        db('jiesuan')->insertGetId($datas[$k]);
                    // }
                }

                if($list_agent){
                    foreach ($list_user as $k=>$v){ 
                        $order_user=db('channel_order')->where(array('state' =>2,'fei_agent_jie' =>0,'agentid'=>$v['agentid']))->whereTime('addtime',$map)->where($where)->where($wheres)->select(); 
                        $money_s[$k]=0;
                        $money_a[$k]=0;
                        $number[$k]=0;
                        foreach ($order_user as $kk=>$vv){
                            $money_s[$k] +=$vv['fei_agent'];
                            $money_a[$k] +=$vv['money'];
                            $number[$k] +=1;
                            db('channel_order')->where('id',$vv['id'])->update(['fei_agent_jie'=>1,'fei_agent_time'=>time()]);
                        } 
                        if($v['agentid']){
                            $username[$k]=db('users')->where('id',$v['agentid'])->value('username');
                            $datas[$k]= array(
                                'orderid' =>'J'.rand_string(8,1).'S'.$v['agentid'], 
                                'money_s' =>$money_s[$k], 
                                'money_a' =>$money_a[$k], 
                                'number' =>$number[$k], 
                                'state' =>0, 
                                'type' =>'agent', 
                                'userid' =>$v['agentid'], 
                                'username' =>$username[$k], 
                                'addtime' =>time(), 
                            ); 
                            db('jiesuan')->insertGetId($datas[$k]);
                        } 
                    }
                    
                }
                

                if($list_user){
                    foreach ($list_user as $k=>$v){ 
                        $order_user=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'userid'=>$v['userid']))->whereTime('addtime',$map)->where($where)->where($wheres)->select(); 
                        $money_s[$k]=0;
                        $money_a[$k]=0;
                        $number[$k]=0;
                        foreach ($order_user as $kk=>$vv){
                            $money_s[$k] +=$vv['fei_user'];
                            $money_a[$k] +=$vv['money'];
                            $number[$k] +=1;
                            db('channel_order')->where('id',$vv['id'])->update(['fei_user_jie'=>1,'fei_user_time'=>time()]);
                        } 
                        $username[$k]=db('users')->where('id',$v['userid'])->value('username');
                        $datas[$k]= array(
                            'orderid' =>'J'.rand_string(8,1).'S'.$v['userid'], 
                            'money_s' =>$money_s[$k], 
                            'money_a' =>$money_a[$k], 
                            'number' =>$number[$k], 
                            'state' =>0, 
                            'type' =>'user', 
                            'userid' =>$v['userid'], 
                            'username' =>$username[$k], 
                            'addtime' =>time(), 
                        ); 
                        db('jiesuan')->insertGetId($datas[$k]);
                    }
                }
                // return $vu['userid'];
                return $result = ['code'=>1,'msg'=>'结算完成!','rel'=>1]; 
            }else{
                return $result = ['code'=>0,'msg'=>'无需结算!','rel'=>1]; 
            }    
        }else{
            return $result = ['code'=>-1,'msg'=>'请正确操作!','rel'=>1]; 
        }
       
       
    }

    //错单处理
    public function no_paystate(){
        if(request()->isPost()){  
            if(db('channel_order')->where(array('id'=>input('id'),'state'=>2))->update(['state'=>0,'points'=>0])!==false){
                return ['code'=>1,'msg'=>'操作成功！'];
            }else{
                return ['code'=>-1,'msg'=>'操作失败！'];
            } 
        }
    }

    //提现申请列表
    public function settle(){
        if(request()->isPost()){ 
            $where=[];
            $map='';
            $shopid = input('post.shopid');
            if($shopid){
                $where['type']='shop';
                $where['typeid']=$shopid;
            }
            $agentid = input('post.agentid');
            if($agentid){
                $where['type']='agent';
                $where['typeid']=$agentid;  
            }
            $userid = input('post.userid');
            if($userid){
                $where['type']='user';
                $where['typeid']=$userid;
            }  
            $timess = input('post.timess');            
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1]);
                if($starttime >= $endtime){
                    return $result = ['code'=>-1,'msg'=>'开始日期要早于结束日期!','rel'=>1];
                }      
                $map= [$starttime,$endtime];  
            }
            $status = input('post.status');
            if($status == 1){
                $where['status']=0;
            }elseif ($status == 2) {
                $where['status']=1;
            }  
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('bill')
                ->where($where)
                ->whereTime('addtime',$map)
                ->order('id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))->toArray(); 
            foreach ($list['data'] as $k=>$v){
                if($v['type']==='user'){
                    $list['data'][$k]['type']='码商'; 
                }elseif ($v['type']==='agent') {
                    $list['data'][$k]['type']='代理'; 
                }elseif ($v['type']==='shop') {
                    $list['data'][$k]['type']='商户';   
                }else{
                    $list['data'][$k]['type']='<span class="red">未知类型</span>'; 
                }
                if($v['paytype']==='bank'){
                    $list['data'][$k]['paytype']='银行卡'; 
                }elseif ($v['paytype']==='wxpay') {
                    $list['data'][$k]['paytype']='微信'; 
                }elseif ($v['paytype']==='alipay') {
                    $list['data'][$k]['paytype']='支付宝';   
                }else{
                    $list['data'][$k]['paytype']=$v['paytype']; 
                }
                if($v['status']==0){
                    $list['data'][$k]['status']='<span class="red">待审核</span>'; 
                }elseif ($v['status']==1) {
                    $list['data'][$k]['status']='<span class="green">审核通过</span>'; 
                }else{
                    $list['data'][$k]['status']='<span class="red">未知类型</span>';   
                }
                $list['data'][$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                if($v['endtime']){
                   $list['data'][$k]['endtime'] = date('Y-m-d H:i:s',$v['endtime']); 
                } 
                // $list['data'][$k]['payname'] = $v['address'].$v['payname'].$v['branchname'];
                // if($v['payname']){
                //     $list['data'][$k]['type']=$address.$v['payname'].$branchname; 
                // }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        if(!$shoplist){
            $shoplist=db('shop')->where('open',1)->order('id')->select();
            cache('shoplist',$shoplist);
        }
        if(!$agentlist){
            $agentlist=db('users')->where('level',2)->where('agentid',0)->order('id')->select();
            cache('agentlist',$agentlist);
        }
        if(!$userlist){
            $userlist=db('users')->where('state',0)->where('is_lock',0)->where('agentid','gt',0)->order('id')->select();
            cache('userlist',$userlist);
        }
        return $this->fetch();
    }

    //处理提现申请
    public function pass_settle(){
        if(request()->isPost()){  
            if(db('bill')->where(array('id'=>input('id'),'status'=>0))->update(['status'=>1,'endtime'=>time()])!==false){
                return ['code'=>1,'msg'=>'操作成功！'];
            }else{
                return ['code'=>-1,'msg'=>'操作失败！'];
            } 
        }
    }

    //统计
     public function total(){
        if(request()->isPost()){ 
            $where=[];
            $map='';
            $shopid = input('post.shopid');
            if($shopid){
                $wheres['type']='shop';
                $wheres['typeid']=$shopid;
                $_wheres['shopid']=$shopid;
            }else{
                $wheres['type']='shop';
            }
            $agentid = input('post.agentid');
            if($agentid){
                $wherea['type']='agent';
                $wherea['typeid']=$agentid;
                $_wherea['agentid']=$agentid;  
            }else{
                $wherea['type']='agent';
            }
            $userid = input('post.userid');
            if($userid){
                $whereu['type']='user';
                $whereu['typeid']=$userid;
                $_whereu['userid']=$userid; 
            }else{
                $whereu['type']='user';
            }  
            $timess = input('post.timess');            
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1]);
                if($starttime >= $endtime){
                    return $result = ['code'=>-1,'msg'=>'开始日期要早于结束日期!','rel'=>1];
                }      
                $map= [$starttime,$endtime];  
            }
            $status = input('post.status');
            if($status == 1){
                $where['status']=0;
            }elseif ($status == 2) {
                $where['status']=1;
            }  
            // $s_time=strtotime(date('Y-m-d',time()));
            // $e_time=$s_time+86399;    
            // $maps= [$s_time,$e_time];  
            $payorder=[];

            //商户
            $payorder['yes']=db('channel_order')->where('state',2)->where($_wheres)->whereTime('addtime',$map)->sum('money');
            $payorder['shop']=db('channel_order')->where('state',2)->where($_wheres)->whereTime('addtime',$map)->sum('fei_shop');
            $payorder['agent']=db('channel_order')->where('state',2)->where($_wheres)->whereTime('addtime',$map)->sum('fei_agent');
            $payorder['user']=db('channel_order')->where('state',2)->where($_wheres)->whereTime('addtime',$map)->sum('fei_user');
            $payorder['shop_yes']=$payorder['yes']- $payorder['shop'];
            $payorder['shop_cash']=db('bill')->where($wheres)->where($where)->whereTime('addtime',$map)->sum('cash');
            $payorder['shop_balance']=number_format($payorder['shop_yes']-$payorder['shop_cash'], 2);

            //代理
            $payorder['agent_yes']=db('channel_order')->where('state',2)->where($_wherea)->whereTime('addtime',$map)->sum('fei_agent');
            $payorder['agent_cash']=db('bill')->where($wherea)->where($where)->whereTime('addtime',$map)->sum('cash');
            $payorder['agent_balance']=$payorder['agent_yes']-$payorder['agent_cash'];

            //码商
            $payorder['user_yes']=db('channel_order')->where('state',2)->where($_whereu)->whereTime('addtime',$map)->sum('fei_user');
            $payorder['user_cash']=db('bill')->where($whereu)->where($where)->whereTime('addtime',$map)->sum('cash');
            $payorder['user_balance']=$payorder['user_yes']-$payorder['user_cash'];
            $payorder['balance']=$payorder['shop']-$payorder['agent']-$payorder['user'];
            $payorders[0]=$payorder;

            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$payorders,'rel'=>1];
        } 
    }

    //补发回调
    public function renotify($orderid)
    {
        $order = db('channel_order')->where('state',2)->where('orderid',$orderid)->find();
        if($order['channelname'] == 'APP'){
            return ['code'=>0,'msg'=>'回调方式未开放！'];
        }else{  
        //创建签名
            if($order){
                $requestarray = array(
                "orderid" =>$order['orderid'],
                "orderid_out" =>$order['orderid_out'],
                "money" => $order['money'], 
                "number" => $order['number'],  
                "version" => $this->system['version'] 
            ); 
            ksort($requestarray); //按照键名进行升序排序 
            $Md5key=db('shop')->where('id',$order['shopid'])->value('appkey');
            $md5str = "";
            foreach ($requestarray as $key => $val) {
                $md5str = $md5str . $key . "=" . $val . "&";
            } 
            $sign = strtoupper(md5($md5str . "key=" . $Md5key));  //签名转大写
            
            $requestarray['sign']=$sign;
            $requestarray['orderid']=$order['orderid']; 
            $requestarray["addtime"] = $order['addtime']; 
            $requestarray["paytime"] = $order['paytime']; 
            $requestarray["state"] = $order['state']; //0未支付，1已支付未确认，2已经确认
            $requestarray["points"] = $order['points']; //0未上分，1自动上分，2手动上分    仅state==2,points==0才可上分               
                if($sign){
                    $notifyurl=$order['notifyurl'];
                    $data_string=json_encode($jsonArr);
                    $curl = curl_init();
                    curl_setopt($curl, CURLOPT_URL,$order['notifyurl']);//设置抓取的url
                    curl_setopt($curl, CURLOPT_HEADER, 'Content-Type:application/x-www-form-urlencoded');//设置头文件的信息作为数据流输出
                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以文件流的形式返回，而不是直接输出。
                    curl_setopt($curl, CURLOPT_POST, 1);//设置post方式提交
                    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
                    curl_setopt($curl, CURLOPT_POSTFIELDS,$md5str."sign=".$requestarray['sign']."&addtime=".$requestarray['addtime']."&paytime=".$requestarray['paytime']."&state=".$requestarray['state']."&points=".$requestarray['points']);
                    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
                    $res_data = curl_exec($curl);//执行命令
                    curl_close($curl);//关闭URL请求   
                    return ['code'=>1,'msg'=>'发送成功！'];
                }else{
                    return ['code'=>0,'msg'=>'发送失败！'];
                } 
            }else{
                return ['code'=>0,'msg'=>'发送失败！'];
            }
        }
    }
}